<?php
	require_once('../libraryfiles/config.php');
	checkAccountType();	
	$content = 'viewprescription.php';
	$include = '
	<script src="'.W_ROOT.'/js/common.js" type="text/javascript"></script>
	<script src="'.W_ROOT.'/js/jquery.js" type="text/javascript"></script>
	';    
	require_once TMP_USER_PATH;
	$pharmacy = $_SESSION['acc_id'];
	$view = isset($_GET['view'])?$_GET['view']:'';
	$order_id = isset($_GET['orderId'])?$_GET['orderId']:'';
	$userId = isset($_GET['userId'])?$_GET['userId']:'';
	$oip_id = isset($_GET['oip_id'])?$_GET['oip_id']:'';
	echo '
		<form action="process.php?action=save" method="post" name="frmlist">
	<div class="block-header">
            <h4>View Prescription</h4>
        </div>';
	switch($view){
		
		// give price
		case'giveprice':
		$sql_d = "SELECT * FROM tblordernance WHERE ordernance_id = $order_id";
		$result_d = mysql_query($sql_d);
		if(dbNumRows($result_d)){
			$row_d = mysql_fetch_assoc($result_d);
			extract($row_d);
		?>
         <script type="text/javascript">
		function checkprice(){
			$('.price').each(function(i){
				if($(this).val()==""){
					alert("Please input price!");
					$(this).focus();
					return false;
				}else{
					//alert("Go");
					document.frmlist.action="process.php?action=send";
					document.frmlist.submit();
				}
			});	
		}
		</script>
        <script type="text/javascript">
		function addS() {
			var t = 0;
			var v = 0;
			$(".price").each(function(){
				if($(this).val() != ""){
					v = $(this).val();
				}else{
					v = 0;
				}
				t += parseFloat(v);
			});
			document.frmlist.txttotal.value=t
		}
		$(function(){
			$(".price").keyup(function(){
				addS();
			});
		});
	</script>
          <div class="rom">
        <p align="center" style="font-size:20px;">Prescription</p>
        <div style="border-top:2px dotted #666; margin:0px 10px 0px 10px;">&nbsp;</div>
        <table cellpadding="5" cellspacing="5" border="0">
        <tbody>
        <tr>
        <td>Patient Name</td><td width="5">:</td><td width="200"><?php echo stripslashes($patient_name);?></td>
        <td align="left">Age</td><td width="5">:</td><td><?php echo $age;?></td>
        
        </tr>
        <tr>
        <td align="left">Sex</td><td width="5">:</td><td><?php echo $sex;?></td>
        <td>Weight</td><td width="5">:</td><td><?php echo $p_weight;?> kg</td>        
        </tr>
        <tr>
        <td align="left">Prescription Number<td width="5">:</td><td><?php echo $ordernance_code;?></td> 
        <td>Height</td><td width="5">:</td><td><?php echo $p_height;?> m</td>
        </tr>
        <tr>
        <td>Check Date</td><td width="5">:</td><td><?php echo $check_date;?></td>       
        <td>Doctor Name</td><td width="5">:</td><td><?php echo $doctor_name;?></td>
        </tr>
        <tr>
        <td valign="top">Note</td>
        <td width="5" valign="top">:</td>
        <td colspan="7">
        <?php echo stripslashes($note);?>
        </td>
        </tr>
        </tbody>
        </table>
        <br />
        <table cellpadding="0" cellspacing="0" border="0" id="displayPro" width="100%" style="padding:0px 2px 0px 2px;">
        <tbody>
            <tr id="displayTR">
            <td width="30" class="col3" align="center">N&deg;</td>
            <td width="80" align="center" class="col3">Image</td>
            <td align="center" width="200" class="col3">Product Name</td>
            <td align="center" width="70" class="col3">Quantity</td>
            <td align="center" class="col3" width="90">Unique Price</td>
            <td align="center" class="col3_1" width="90">Price</td>
            </tr>
            <?php
			$i=1;
			$sql = "SELECT * FROM tblproduct_in_ordernance WHERE order_id = $order_id";
			$result = mysql_query($sql);
			while($row = mysql_fetch_assoc($result)){
			$sql_img = "SELECT thumbnail FROM tblimage WHERE product_id = ".$row['product_id']." LIMIT 0,1";
				$result_img = mysql_query($sql_img);
				$row_img = mysql_fetch_assoc($result_img);
			if($row_img['thumbnail']!=""){
					$thumbnail_i = 	$row_img['thumbnail'];
				}else{
					$thumbnail_i = 	'no-image-small.png';
				}
			$sql_p = "SELECT id,pharma_check FROM tblordernance_in_pharmacy WHERE ordernance_id = $order_id AND pharmacy_id = $pharmacy";
			$result_p = mysql_query($sql_p);
			$row_oip = mysql_fetch_assoc($result_p);
			
			$sql_price = "SELECT unique_price, price FROM tblpro_order_in_pharmacy WHERE oip_id=".$row_oip['id']." AND pio_id = ".$row['id'];
			$result_price = mysql_query($sql_price);
			$row_price = mysql_fetch_assoc($result_price);	
			?>
            <tr>
            <td width="30" class="col4" align="center"><?php echo idFormat($i);?></td>
            <td width="80" align="center" class="col4"><img src="<?php echo W_ROOT;?>/seller/product/images/<?php echo $thumbnail_i; ?>" class="border" width="50" /></td>
            <td align="center" width="200" class="col4"><strong style="color:#06C;"><?php echo getProductName($row['product_id'])?></strong><br />
            <em style="color:#666;">Made in : <?php echo getCountryName($row['made_in']);?></em>
            </td>
            <td align="center" width="70" class="col4">
            <?php
			echo $row['quatity'];
			?>
            </td>
            <td align="center" class="col4">$ <input type="text" style="width:50px;" name="txtuniquep[]" class="uniqueprice" value="<?php echo $row_price['unique_price']?>" /></td>
            <td align="center" class="col4_1">$ <input type="text" style="width:50px;" name="txtprice[]" class="price" value="<?php echo $row_price['price']?>" />
            <input type="hidden" name="hidepio_id[]" value="<?php echo $row['id']?>" />
            <input type="hidden" name="hideoip_id[]" value="<?php echo $oip_id;?>" />            
            </td>
	        </tr>
            <?php
			$i++;
			}
			?>
            <tr>
            <td colspan="5" align="right" style="padding-top:10px;"><strong>Total :</strong></td>
            <td align="center" style="padding-top:10px;">$ <input type="text" name="txttotal" style="width:70px;" /></td>
            </tr>
            <tr>
            <td colspan="6" style="padding:10px 2px 10px 0px;" align="right">
            <input type="button" name="btnsend" value="Send" onclick="checkprice();" />&nbsp;
            <input type="submit" name="btnsave" value="Save" />&nbsp;
            <input type="button" name="btnback" value="Back" onclick="window.location.href='viewprescription.php'" />
            <input type="hidden" name="hideorder_id" value="<?php echo $order_id;?>" />
            </td>
            </tr>
        </tbody>
        </table>
        </div>
        <?php
		}
		break;
		// detail
		case'detail':
		$sql_d = "SELECT * FROM tblordernance WHERE ordernance_id = $order_id";
		$result_d = mysql_query($sql_d);
		if(dbNumRows($result_d)){
			$row_d = mysql_fetch_assoc($result_d);
			extract($row_d);
		?>
        <div class="center">
        <p align="center" style="font-size:20px;">Prescription</p>
        <div style="border-top:2px dotted #666; margin:0px 10px 0px 10px;">&nbsp;</div>
        <table cellpadding="5" cellspacing="5" border="0">
        <tbody>
        <tr>
        <td>Patient Name</td><td width="5">:</td><td width="200"><?php echo stripslashes($patient_name);?></td>
        <td align="left">Age</td><td width="5">:</td><td><?php echo $age;?></td>
        
        </tr>
        <tr>
        <td align="left">Sex</td><td width="5">:</td><td><?php echo $sex;?></td>
        <td>Weight</td><td width="5">:</td><td><?php echo $p_weight;?> kg</td>        
        </tr>
        <tr>
        <td align="left">Prescription Number<td width="5">:</td><td><?php echo $ordernance_code;?></td> 
        <td>Height</td><td width="5">:</td><td><?php echo $p_height;?> m</td>
        </tr>
        <tr>
        <td>Check Date</td><td width="5">:</td><td><?php echo $check_date;?></td>       
        <td>Doctor Name</td><td width="5">:</td><td><?php echo $doctor_name;?></td>
        </tr>
        <tr>
        <td valign="top">Note</td>
        <td width="5" valign="top">:</td>
        <td colspan="7">
        <?php echo stripslashes($note);?>
        </td>
        </tr>
        </tbody>
        </table>
        <br />
        <table cellpadding="0" cellspacing="0" border="0" id="displayPro" width="100%" style="padding:0px 2px 0px 2px;">
        <tbody>
            <tr id="displayTR">
            <td width="30" class="col3" align="center">N&deg;</td>
            <td width="80" align="center" class="col3">Image</td>
            <td align="center" width="200" class="col3">Product Name</td>
            <td align="center" width="70" class="col3">Quantity</td>
            <td align="center" class="col3" width="90">Unique Price</td>
            <td align="center" class="col3_1" width="90">Price</td>
            </tr>
            <?php
			$i=1;
			$sql = "SELECT * FROM tblproduct_in_ordernance WHERE order_id = $order_id";
			$result = mysql_query($sql);
			while($row = mysql_fetch_assoc($result)){
			$sql_img = "SELECT thumbnail FROM tblimage WHERE product_id = ".$row['product_id']." LIMIT 0,1";
				$result_img = mysql_query($sql_img);
				$row_img = mysql_fetch_assoc($result_img);
			if($row_img['thumbnail']!=""){
					$thumbnail_i = 	$row_img['thumbnail'];
				}else{
					$thumbnail_i = 	'no-image-small.png';
				}
			$sql_p = "SELECT id,pharma_check,total_price FROM tblordernance_in_pharmacy WHERE ordernance_id = $order_id AND pharmacy_id = $pharmacy";
			$result_p = mysql_query($sql_p);
			$row_oip = mysql_fetch_assoc($result_p);
			
			$sql_price = "SELECT unique_price, price FROM tblpro_order_in_pharmacy WHERE oip_id=".$row_oip['id']." AND pio_id = ".$row['id'];
			$result_price = mysql_query($sql_price);
			$row_price = mysql_fetch_assoc($result_price);
			?>
            <tr>
            <td width="30" class="col4" align="center"><?php echo idFormat($i);?></td>
            <td width="80" align="center" class="col4"><img src="<?php echo W_ROOT;?>/seller/product/images/<?php echo $thumbnail_i; ?>" class="border" width="50" /></td>
            <td align="center" width="200" class="col4"><strong style="color:#06C;"><?php echo getProductName($row['product_id'])?></strong><br />
            <em style="color:#666;">Made in : <?php echo getCountryName($row['made_in']);?></em>
            </td>
            <td align="center" width="70" class="col4">
            <?php
			echo $row['quatity'];
			?>
            </td>
            <td align="center" class="col4">
            <?php
			if($row_price['unique_price']!=""){
			?>
            $ <?php echo $row_price['unique_price']?>
            <?php
			}
			?>
            </td>
            <td align="center" class="col4_1">
            <?php
			if($row_price['price']!=""){
			?>
            $ <?php echo $row_price['price']?>
            <?php
			}
			?>
            </td>
	        </tr>
            <?php
			$i++;
			}
			?>
            <tr>
            <td colspan="5" align="right" style="padding-top:10px;"><strong>Total :</strong></td>
            <td align="center" style="padding-top:10px;">
            $ <?php echo $row_oip['total_price']?>
            </td>
            </tr>
            <tr>
            <td colspan="6" style="padding:10px 2px 10px 0px;" align="right">
            <?php
			if($row_oip['pharma_check']==0){
			?>
            <input type="button" name="btngiveprice" value="Give Price" onclick="window.location.href='viewprescription.php?view=giveprice&orderId=<?php echo $order_id?>&amp;userId=<?php echo $userId;?>&amp;oip_id=<?php echo $oip_id?>'" />&nbsp;
            <?php
			}
			?>
            <input type="button" name="btnback" value="Back" onclick="window.location.href='viewprescription.php'" />
            </td>
            </tr>
        </tbody>
        </table>
        </div>
        <?php
		}
		break;
		
		// list
		default:
		$sql_l = "SELECT o.ordernance_id, o.user_id, o.cdate, op.* FROM tblordernance AS o,tblordernance_in_pharmacy AS op WHERE op.pharmacy_id = $pharmacy AND o.ordernance_id = op.ordernance_id AND op.isDeleted = 0 ORDER BY o.cdate DESC";
		$result_l = mysql_query($sql_l);
		?>
        <div class="center">
        <p align="right" style="padding-right:2px;">
        <input type="button" name="btnunpublish" value="Delete" onclick="deleteSelected(document.frmlist,'<?php echo htmlspecialchars(getAllGetParams(array('action','view')));?>','Prescription(s)','process');" />
        </p>
        <table cellpadding="0" cellspacing="0" border="0" id="displayPro" width="100%" style="padding:0px 2px 0px 2px;">
        <tbody>
            <tr>
            <td width="40" class="col3" align="center">N&deg;</td>
            <td width="20" class="col3" align="center"><input type="checkbox" name="chkall" id="chkall" onClick="checkAll(this.form,this.checked)" style="cursor:pointer;"></td>
            <td width="290" align="left" style="padding-left:15px;" class="col3">Customer Name</td>
            <td align="center" width="100" class="col3">View</td>
            <td align="center" width="100" class="col3_1">Status</td>
            </tr>
            <?php
			$i = 1;
			while($row_l = mysql_fetch_assoc($result_l)){
				extract($row_l);
			?>
            <tr>
            <td class="col4" align="center"><?php echo idFormat($i);?></td>
            <td class="col4" align="center"><input type="checkbox" name="id[]" value="<?php echo $id;?>"/>
            </td>
            <td class="col4" align="left" style="padding-left:15px;">
			<?php echo getUserName($user_id);?><br />
            <em style="color:#666;">Requested Date: <?php echo $cdate?></em>
            </td>
            <td class="col4" align="center"><a href="<?php $_SERVER['PHP_SELF']?>?view=detail&orderId=<?php echo $ordernance_id?>&amp;userId=<?php echo $user_id;?>&amp;oip_id=<?php echo $id;?>" style="color:#03F;">View</a></td>
            <td class="col4_1" align="center">
            <?php
			if($pharma_check==0){
			?>
            <a href="<?php $_SERVER['PHP_SELF']?>?view=giveprice&orderId=<?php echo $ordernance_id?>&amp;userId=<?php echo $user_id;?>&amp;oip_id=<?php echo $id;?>" style="color:#03F;">Give Price</a>
            <?php	
			}else{
			echo '<span style="color:#999;">Done</span>';	
			}
            ?>
			</td>   
            </tr>
            <?php
			$i++;
			}
			?>
        <tbody>
        </table>
        <br />
        </div>
        
        <?php
		break;
	}
?>
</form>
</div>